/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package algoritmogeneticoCHC;

/**
 * This class keeps track of the bit count for the ingested arrays
 * where the value to sum is when value equals 1
 * @author Zal
 */
public class SchemaTool {
    public long[] BitCountArr;
    public long TotalSums;
    private double Threshold;
    private double UpThold;
    private double LoThold;

    public SchemaTool(int size) {
        TotalSums = 0;
        BitCountArr = new long[size];
        Threshold = 0.025;
        UpThold = 0.975;
        LoThold = 0.025;
    }
        
    public boolean sum(double[] arr) {
        if( arr.length != BitCountArr.length) {
            return false;
        }
        
        for (int i = 0; i < BitCountArr.length; i++) {
            if( arr[i] == 1 ) {
                BitCountArr[i]++;
            }
        }
        TotalSums++;
        return true;
    }
       
    public boolean sum(int[] arr) {
        if( arr.length != BitCountArr.length) {
            return false;
        }
        
        for (int i = 0; i < BitCountArr.length; i++) {
            if( arr[i] == 1 ) {
                BitCountArr[i]++;
            }
        }
        TotalSums++;
        return true;
    }
    
    public boolean sum(SchemaTool o) {
        if( o.BitCountArr.length != BitCountArr.length) {
            return false;
        }
        
        for (int i = 0; i < BitCountArr.length; i++) {
                BitCountArr[i] += o.BitCountArr[i];
        }
        TotalSums += o.TotalSums;
        return true;
    }
    
    /*
     * Gives the sum at a given index of all the arrays added
     */
    public double getCount(int bitPos) {
        if( TotalSums == 0)
            return 0;
        
        return BitCountArr[bitPos];
    }
    
    /*
     * Gives the proportion at a given index of all the arrays 
     * added to this object
     */
    public double getProportion(int bitPos) {
        if( TotalSums == 0)
            return 0;
        
        return (double)BitCountArr[bitPos]/(double)TotalSums;
    }
    
    public String toString() {
        String result = getSchemaChar(0);
        for (int i = 1; i < BitCountArr.length; i++) {
            result+= getSchemaChar(i);
        }
        return result;
    }
    
    public void setThreshold(double threshold) {
        Threshold = threshold;
        LoThold = threshold;
        UpThold = 1 - threshold;
    }
    private String getSchemaChar(int pos) {
        double bitProportion = getProportion(pos);
        if( bitProportion <= LoThold) {
            return "0";
        }
        if( bitProportion >= UpThold) {
            return "1";
        }
        return "*";
    }
    
}